/**
 * Created with JetBrains WebStorm.
 * User: azusdex
 * Date: 09/01/14
 * Time: 22:49
 * To change this template use File | Settings | File Templates.
 */
//var url = "http://localhost/travis/web/frontend_dev.php/";
var url = "http://azusdex.webcopo.com/travis/web/frontend_dev.php/";

function login(username, password) {
    var login = $.ajax({
        type: "POST",
        url: url + "login",
        data: {username: username, password: password}
    });

    login.done(function(msg) {
        alert(msg);
    });

    login.fail(function(msg) {
        alert(msg);
    });
}

function reset(username) {
    var reset = $.ajax({
        type: "POST",
        url: url + "reset",
        data: {username: username},
        beforeSend: function() {
            showLoader();
        },
        success: function(msg) {
            hideLoader();
            showResult(msg);
        }
    });
}

function signup(data) {
    var signup = $.ajax({
       type: "POST", 
       url: url + "signup",
       data: data,
       beforeSend: function() {
           showLoader();
       },
       success: function(msg) {
           hideLoader();
           showResult(msg);
       }
    });
}

function showResult(msg) {
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    $('#blackout').css({'width': winWidth + 'px', 'height': winHeight + 'px'});
    $('#blackout').show();

    var boxWidth = 400;
    var disWidth = (winWidth - boxWidth) / 2
    var disHeight = 150;
    $('.popup_result').css({'width': boxWidth + 'px', 'left': disWidth + 'px', 'top': disHeight + 'px'});
    $('.popup_result').show();

    $('.popup_result_status').empty();
    $('.popup_result_text').empty();

    var json = $.parseJSON(msg);
    $(json).each(function(i, v) {
        if (v.status == 'success') {
            $('.popup_result_status').css({color: '#11BE2F'});
        } else {
            $('.popup_result_status').css({color: '#BE1140'});
        }
        
        $('.popup_result_status').append(v.status);
        $('.popup_result_text').append(v.message);
    });
}

function hideResult() {
    $('#blackout').css({'width': 0 + 'px', 'height': 0 + 'px'});
    $('#blackout').hide();

    $('.popup_result').hide();
}

function showLoader() {
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    $('#blackout').css({'width': winWidth + 'px', 'height': winHeight + 'px'});
    $('#blackout').show();

    var boxWidth = 220;
    var disWidth = (winWidth - boxWidth) / 2;
    var disHeight = 200;
    $('.loader').css({'width': boxWidth + 'px', 'left': disWidth + 'px', 'top': disHeight + 'px'});
    $('.loader').show();
}

function hideLoader() {
    $('#blackout').css({'width': 0 + 'px', 'height': 0 + 'px'});
    $('#blackout').hide();

    $('.loader').hide();
}